package org.ihelpuoo.weboo.security;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.util.Properties;
import org.ihelpuoo.weboo.module.ModuleContext;
import org.ihelpuoo.weboo.module.ModuleInstaller;

public class SercurityInstaller implements ModuleInstaller
{
	private static Logger	log	= Logger.getLogger(SercurityInstaller.class);

	public void install(ModuleContext context) throws Exception
	{
		String v = context.getConfig().getStringTrimed("<ROLES>");
		Security security = Security.me(context.nut());
		if (!StringUtils.isBlank(v))
		{
			Properties p = new Properties(v);
			Iterator<String> it = p.keySet().iterator();
			while (it.hasNext())
			{
				String name = it.next();
				String value = p.getStringTrimed(name);
				security.addRole(name,value);
			}
		}
		security.setFailTarget(context.getConfig().getStringTrimed("security.redirect"));
		if (log.isInfoEnabled())
			log.info("Init " + security.getRoleSize() + " roles");
	}

}
